// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Classification;
import com.liuxinlong.modules.system.controller.request.ImportClassificationReq;
import com.liuxinlong.modules.system.controller.request.QueryUserReq;
import com.liuxinlong.modules.system.controller.request.UpdateClassificationReq;
import com.liuxinlong.modules.system.service.ClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标准分类信息控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/classification")
public class ClassificationController {

    @Autowired
    private ClassificationService classificationService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询标准分类信息
     *
     * @param req 请求信息
     * @return 标准分类信息列表
     */
    @PostMapping("page")
    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.STANDARD_CLASSIFICATION,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageClassificationInfo(@RequestBody QueryUserReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("name", req.getName());
        List<Map<String, Object>> dataList = classificationService.pageClassificationInfo(queryMap);
        int total = classificationService.countClassification(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询标准分类信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 新增标准分类信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增标准分类信息", business = LogBusinessConstants.STANDARD_CLASSIFICATION,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addClassificationInfo(@RequestBody UpdateClassificationReq req) {
        Classification classification = new Classification();
        classification.setName(req.getName());
        classification.setParentId(req.getParent());
        classification.setShowIndex(req.getShowIndex());
        classification.setRemark(req.getRemark());
        classificationService.addClassificationInfo(classification);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增标准分类信息成功");
        return resp;
    }

    /**
     * 修改标准分类信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改标准分类信息", business = LogBusinessConstants.STANDARD_CLASSIFICATION,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateClassificationInfo(@RequestBody UpdateClassificationReq req) {
        Classification classification = new Classification();
        classification.setId(req.getId());
        classification.setName(req.getName());
        classification.setParentId(req.getParent());
        classification.setShowIndex(req.getShowIndex());
        classification.setRemark(req.getRemark());
        classificationService.updateClassificationInfo(classification);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改标准分类信息成功");
        return resp;
    }

    /**
     * 删除标准分类信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除标准分类信息", business = LogBusinessConstants.STANDARD_CLASSIFICATION,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteClassificationInfo(@RequestBody BaseDeleteReq req) {
        classificationService.deleteClassificationInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除标准分类信息成功");
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入标准分类数据", business = LogBusinessConstants.STANDARD_CLASSIFICATION,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importClassifications(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = classificationService.importClassifications((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入标准分类信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入标准分类数据", business = LogBusinessConstants.STANDARD_CLASSIFICATION,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddClassification(@RequestBody ImportClassificationReq req) {
        return classificationService.batchAddClassification(req.getDetailList());
    }
}
